package logic

import (
	"context"
	"fmt"
	"taobao/goods_srv/model/mysql"
	"taobao/goods_srv/model/redis"

	"taobao/goods_srv/internal/svc"
	"taobao/goods_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginByMobileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginByMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginByMobileLogic {
	return &UserLoginByMobileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginByMobileLogic) UserLoginByMobile(in *user.UserLoginByMobileRequest) (*user.UserNameInfoResponse, error) {
	// todo: 手机号登录

	u := mysql.UserWeek{}
	err := u.GetByMobileInfo(in.Mobile)
	if err != nil {
		logx.Error("用户信息获取失败", err)
		return nil, err
	}
	//获取缓存的验证码
	code := redis.RedisGetSmsCode(in.Mobile, "login")

	fmt.Print("验证码是..............", code)

	//销毁短信
	redis.RedisDelSmsCode(in.Mobile, "login")
	return &user.UserNameInfoResponse{
		UserId:   int64(u.ID),
		Username: u.Username,
		Password: u.Password,
		Image:    u.Avatar,
		Code:     code,
	}, nil
}
